1.Types of spatial data

1.1 Areal data

# To install stringi package by hands for Rmarkdown. Installing sf package required prior installed Rtools.
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(mapview)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
library(stringi)
## Warning: le package 'stringi' a été compilé avec la version R 4.4.0
d <- st_read(system.file("shape/nc.shp", package = "sf"),
             quiet = TRUE)
mapview(d, zcol = "SID74")
library(spData)
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(ggplot2)
d <- st_read(system.file("shapes/columbus.shp", package = "spData"), quiet = TRUE)
ggplot(d) + geom_sf(aes(fill = INC))

library(terra)
## Warning: le package 'terra' a été compilé avec la version R 4.4.0
## terra 1.7.46
d <- rast(system.file("ex/elev.tif", package = "terra"))
plot(d)

1.2 Geostatistical data

library(sp)
library(sf)
library(mapview)

data("meuse")
meuse <- st_as_sf(meuse, coords = c("x", "y"),  
                  crs = 28992)
mapview(meuse, zcol = "lead", map.types = "CartoDB.Voyager")
library(spData)#To access larger datasets in this package,install the spDataLarge package with
install.packages('spDataLarge',repos='https://nowosad.github.io/drat/',type='source')
## Installation du package dans 'C:/Users/ERNEST/AppData/Local/R/win-library/4.3'
## (car 'lib' n'est pas spécifié)
## Warning in download.file(url, destfile, method, mode = "wb", ...): taille
## téléchargée 0 != taille déclarée 0
## Warning in download.file(url, destfile, method, mode = "wb", ...): URL
## ‘https://jakubnowosad.com/drat/src/contrib/spDataLarge_2.1.1.tar.gz’: délais
## maximal de 60 secondes atteint
## Error in download.file(url, destfile, method, mode = "wb", ...) : 
##   le téléchargement de 'https://nowosad.github.io/drat/src/contrib/spDataLarge_2.1.1.tar.gz' a échoué
## Warning in download.packages(pkgs, destdir = tmpd, available = available, : le
## téléchargement du package 'spDataLarge' a échoué
mapview(properties, zcol = "prpsqm")
library(malariaAtlas)
#d <- getPR(country = "Zimbabwe", species = "BOTH")
#ggplot2::autoplot(d)
library(spatstat)
## Le chargement a nécessité le package : spatstat.data
## Le chargement a nécessité le package : spatstat.geom
## spatstat.geom 3.2-7
## 
## Attachement du package : 'spatstat.geom'
## Les objets suivants sont masqués depuis 'package:terra':
## 
##     area, delaunay, rescale, rotate, shift, where.max, where.min
## Le chargement a nécessité le package : spatstat.random
## spatstat.random 3.1-6
## Le chargement a nécessité le package : spatstat.explore
## Le chargement a nécessité le package : nlme
## spatstat.explore 3.2-3
## Le chargement a nécessité le package : spatstat.model
## Le chargement a nécessité le package : rpart
## spatstat.model 3.2-6
## Le chargement a nécessité le package : spatstat.linnet
## spatstat.linnet 3.1-1
## 
## spatstat 3.0-6 
## For an introduction to spatstat, type 'beginner'
plot(clmfires, use.marks = FALSE, pch = ".")

plot(hamster)

library(sparr)
## 
## 
## Welcome to
##    _____ ___  ____  ____  ____         
##   / ___// _ \/ _  \/ __ \/ __ \        
##   \__ \/ ___/ __  /  ___/  ___/        
##  ___/ / /  / / / / /\ \/ /\ \          
## /____/_/  /_/ /_/_/  \__/  \_\   v2.3-10
## 
## - type news(package="sparr") for an overview
## - type help("sparr") for documentation
## - type citation("sparr") for how to cite
data(pbc)
plot(unmark(pbc[which(pbc$marks == "case"), ]), main = "cases")
axis(1)
axis(2)
title(xlab = "Easting", ylab = "Northing")

plot(unmark(pbc[which(pbc$marks == "control"), ]), pch = 3, main = "controls")
axis(1)
axis(2)
title(xlab = "Easting", ylab = "Northing")

library(SpatialEpiApp)
library(sf)
library(ggplot2)
library(viridis)
## Le chargement a nécessité le package : viridisLite
# map
f <- file.path("SpatialEpiApp/data/Ohio/fe_2007_39_county/", "fe_2007_39_county.shp")
pathshp <- system.file(f, package = "SpatialEpiApp")
map <- st_read(pathshp, quiet = TRUE)


# data
namecsv <- "SpatialEpiApp/data/Ohio/dataohiocomplete.csv"
d <- read.csv(system.file(namecsv, package = "SpatialEpiApp"))

# data are disaggregated by gender and race
# aggregate to get population in each county and year
d <- aggregate(x = d$n, by = list(county = d$NAME, year = d$year), FUN = sum)
names(d) <- c("county", "year", "population")

# join map and data
mapst <- dplyr::left_join(map, d, by = c("NAME" = "county"))

# map population by year
# facet_wrap() splits data into subsets and create multiple plots
ggplot(mapst, aes(fill = log(population))) + geom_sf() + 
facet_wrap(~ year, ncol = 7) + scale_fill_viridis("log(population)") +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank())

library(sf)
library(geoFourierFDA)
## 
## Attachement du package : 'geoFourierFDA'
## L'objet suivant est masqué depuis 'package:stats':
## 
##     logLik
library(rnaturalearth)
## Support for Spatial objects (`sp`) will be deprecated in {rnaturalearth} and will be removed in a future release of the package. Please use `sf` objects with {rnaturalearth}. For example: `ne_download(returnclass = 'sf')`
library(rnaturalearthhires)#installed by hands
library(ggplot2)


# Map Canada
map <- rnaturalearth::ne_states("Canada", returnclass = "sf")

# Coordinates of stations
d <- data.frame(canada$m_coord)
d$location <- attr(canada$m_coord, "dimnames")[[1]]
d <- st_as_sf(d, coords = c("W.longitude", "N.latitude"))
st_crs(d) <- 4326

# Plot Canada map and location of stations
ggplot(map) + geom_sf() + geom_sf(data = d, size = 6) +
geom_sf_label(data = d, aes(label = location), nudge_y = 2)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

# Temperature of each station over time
d <- data.frame(canada$m_data)
d$time <- 1:nrow(d)

# Pivot data d from wide to long
# cols: columns to pivot in longer format
# names_to: name of new column with column names of original data
# values_to: name of new column with values of original data
df <- tidyr::pivot_longer(data = d, cols = names(d)[-which(names(d) == "time")], names_to = "variable", values_to = "value")

# Plot temperature of each station over time
ggplot(df, aes(x = time, y = value)) +
geom_line(aes(color = variable))

library(epiflows)
## epiflows is loaded with the following global variables in `global_vars()`:
## coordinates, pop_size, duration_stay, first_date, last_date, num_cases
library(igraph)#This package is required to allow epiflows package runs
## 
## Attachement du package : 'igraph'
## Les objets suivants sont masqués depuis 'package:spatstat.geom':
## 
##     diameter, edges, is.connected, vertices
## Les objets suivants sont masqués depuis 'package:terra':
## 
##     blocks, compare, union
## Les objets suivants sont masqués depuis 'package:stats':
## 
##     decompose, spectrum
## L'objet suivant est masqué depuis 'package:base':
## 
##     union
data("Brazil_epiflows")

loc <- merge(x = YF_locations, y = YF_coordinates, by.x = "location_code", by.y = "id", sort = FALSE)

ef <- make_epiflows(flows = YF_flows, locations = loc,
                    coordinates = c("lon", "lat"),
                    pop_size = "location_population",
                    duration_stay = "length_of_stay",
                    num_cases = "num_cases_time_window",
                    first_date = "first_date_cases",
                    last_date = "last_date_cases")



vis_epiflows(ef)
map_epiflows(ef)